package com.banzhi.shop.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.R;
import com.banzhi.shop.activity.ChoicesDetailsActivity;
import com.banzhi.shop.adapter.MyChoiceAdapter;
import com.banzhi.shop.data.ChoiceData;
import com.banzhi.shop.entity.RefundRequest.Refund_Data;
import com.banzhi.shop.model.CallBack.ChoiceCallBack;
import com.banzhi.shop.model.Impl.ChoiceImpl;
import com.banzhi.shop.model.Impl.GroupingImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.EndlessRecyclerOnScrollListener;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/11/9.
 */

public class ChoiceFragments extends Fragment implements ChoiceCallBack {


    private String mType;
    private RecyclerView mRecyclerView;
    private SmartRefreshLayout mRefreshLayout;
    private ChoiceImpl mModel;
    private int page = 1;
    private ChoiceData mChoiceData;
    private BaseQuickAdapter<ChoiceData.GoodsBean, BaseViewHolder> mAdapter;
    private Receiver mReceiver;
    List<ChoiceData.GoodsBean> GoodsBeans = new ArrayList<>();
    private MyChoiceAdapter mMyChoiceAdapter;
    private GridLayoutManager mGridLayoutManager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragments_choice,container,false);

      //获取传过来的fragment的id
        Bundle bundle = this.getArguments();
        if (bundle != null){
            mType = bundle.getString("type");
        }
        mModel = new ChoiceImpl();
        mModel.getData(mType+"",page+"",20+"",this);
        initView(view);


        //注册广播接收者
        mReceiver = new Receiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("Refresh2");
        getActivity().registerReceiver( mReceiver, intentFilter);
        return view;
    }

    private void initView(View view) {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerViews);
        mGridLayoutManager = new GridLayoutManager(getActivity(), 2);
        mRecyclerView.setLayoutManager(mGridLayoutManager);
        mRecyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(mGridLayoutManager) {
            @Override
            public void onLoadMore() {
                page++;
                LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
                mModel.getData(mType+"",page+"",20+"",ChoiceFragments.this);
            }
        });
        mRefreshLayout = (SmartRefreshLayout) view.findViewById(R.id.smart_refresh_layout);
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mModel.getData(mType+"",page+"",20+"",ChoiceFragments.this);
            }
        });
    }

    public static ChoiceFragments newInstance(String type) {
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        ChoiceFragments choiceFragments = new ChoiceFragments();
        choiceFragments.setArguments(bundle);
        return choiceFragments;
    }

    @Override
    public void ongetChoiceDataSuccess(Object object) {
        mChoiceData = (ChoiceData) object;

        List<ChoiceData.GoodsBean> goods = mChoiceData.getGoods();
      //  mAdapter.setNewData(goods);
        if (page==1) {
         GoodsBeans.clear();
       //   mAdapter.setNewData(goods);
       //     LogUtils.e("TAG111111",goods.toString());
        }else {
            if (mChoiceData.getGoods().size()==0) {
                return;
            }
   //      mAdapter.addData(goods);
        }
        GoodsBeans.addAll(goods);
        setView();

    }

    private void setView() {
        mMyChoiceAdapter = new MyChoiceAdapter(GoodsBeans);
        mRecyclerView.setAdapter(mMyChoiceAdapter);
        mMyChoiceAdapter.setOnItemClickListener(new MyChoiceAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
              //  ToastUtils.makeToastShort(""+position);
                Intent intent = new Intent(getActivity(), ChoicesDetailsActivity.class);
                intent.putExtra("id",GoodsBeans.get(position).getId());
                startActivity(intent);

            }
        });
    }

    @Override
    public void ongetChoiceDataError(Object object) {

    }
    public class Receiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if ("Refresh2".equals(action)) {
                //  setRefreshListener();
                mModel.getData(mType+"",page+"",20+"",ChoiceFragments.this);
              //  mPinTuan.getData(mType + "", page + "", 20 + "", MyGroupingFragments.this);
            }
        }
    }

    @Override
    public void onDestroy() {
        //注销广播接受者
        getActivity().unregisterReceiver( mReceiver);
        super.onDestroy();
    }
}
